﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace CapaPresentacion.Controles
{
    /// <summary>
    /// Lógica de interacción para CargadorLista.xaml
    /// </summary>
    public partial class CargadorLista : UserControl
    {

        public static readonly DependencyProperty FuenteDatosProperty = DependencyProperty.Register("FuenteDatos", typeof(ObservableCollection<string>), typeof(CargadorLista));
        public static readonly DependencyProperty TextoAMostrarProperty = DependencyProperty.Register("TextoAMostrar", typeof(string), typeof(CargadorLista));


        public CargadorLista()
        {
            InitializeComponent();

            Binding b1 = new Binding();
            b1.Source = this;
            b1.Path = new PropertyPath(CargadorLista.FuenteDatosProperty);
            b1.Mode = BindingMode.TwoWay;
            this.listaDatos.SetBinding(ListBox.ItemsSourceProperty, b1);

            Binding b2 = new Binding();
            b2.Source = this;
            b2.Path = new PropertyPath(CargadorLista.TextoAMostrarProperty);
            b2.Mode = BindingMode.TwoWay;
            this.listaDatos.SetBinding(ListBox.DisplayMemberPathProperty, b2);


        }


        public string Etiqueta
        {
            get { return lbl.Content.ToString(); }

            set { lbl.Content = value; }
        }

        private void btnAgregar_Click(object sender, RoutedEventArgs e)
        {
            if (txtDato.Text  != String.Empty)
            {
                bool existe = false;
                foreach (var v in this.FuenteDatos)
                {
                    if (v.ToLower() == this.txtDato.Text.Trim().ToLower())
                        existe = true;

                }
                if (!existe)
                {
                    this.FuenteDatos.Add(this.txtDato.Text.Trim());
                    this.txtDato.Text = String.Empty;
                }


            }
        }

        private void btnQuitar_Click(object sender, RoutedEventArgs e)
        {
            if (this.listaDatos.SelectedIndex != -1)
                this.FuenteDatos.RemoveAt(this.listaDatos.SelectedIndex);

        }


        public ObservableCollection<string> FuenteDatos
        {
            get
            {
                return (ObservableCollection<string>)this.GetValue(FuenteDatosProperty);
            }

            set { SetValue(FuenteDatosProperty, value); }

        }

        public string TextoAMostrar
        {
            get { return (string)GetValue(TextoAMostrarProperty); }
            set { SetValue(TextoAMostrarProperty, value); }
        }


    }
}
